<?
include_once("_common.php");
include_once("$mw_cash[path]/cybercash.lib.php");

$g4[title] = "$board[bo_subject] 멤버쉽 결제 ({$mi_day}일)";
include_once("$g4[path]/head.sub.php");

if (!$is_member)
    alert_close("로그인 후 이용하실 수 있습니다.");

$row = sql_fetch("select * from $mw_cash[membership_table] where ms_id = '$ms_id'");
$ms_limit_day = $row[ms_limit_day];
if ($ms_limit_day) {
    // 아직 유효한 멤버쉽이 있다면 종료날짜 + 추가기간
    $sql = "select *
              from $mw_cash[membership_log_table]
             where mb_id = '$member[mb_id]'
               and ml_edate >= '$g4[time_ymdhis]'
               and ms_id = '$ms_id'
             order by ml_edate desc limit 1";
    $row = sql_fetch($sql);

    if ($row) {
        $ml_sdate = date("Y-m-d H:i:s", strtotime($row[ml_edate]) + (60*60*24));
        $ml_edate = date("Y-m-d H:i:s", strtotime($ml_sdate) + ($mi_day * 60*60*24));
        $limit_day = date("Y-m-d H:i:s", $g4[server_time] + ($ms_limit_day * 60*60*24));

        if ($ml_edate > $limit_day)
            alert("죄송합니다. {$ms_limit_day}일 이상 연장하실 수 없습니다.");
    }
}

$edate = false;
$sql = "select * from $mw_cash[membership_log_table] where ms_id = '$ms_id' and mb_id = '$member[mb_id]' and ml_edate >= '$g4[time_ymdhis]' ";
$sql.= "order by ml_edate desc limit 1";
$row = sql_fetch($sql);
if ($row) $edate = $row[ml_edate];

$row = sql_fetch("select * from $mw_cash[membership_item_table] where ms_id = '$ms_id' and mi_day = '$mi_day'");
if (!$row)
    alert("멤버쉽과 기간을 선택해주세요.");

$cash = $row[mi_cash];

if ($edate) { // 연장할인
    if ($row[mi_cash_extend] > $discount) {
        $discount = $row[mi_cash_extend];
        $cash = round($row[mi_cash] * ((100-$discount)/100));
    }
}

// 장기회원 할인
if ($row[mi_cash_join_day] && $row[mi_cash_join_sale] > $discount && $row[mi_cash_join_day] < ($g4[server_time]-strtotime($member[mb_datetime]))/(60*60*24)) {
    $discount = $row[mi_cash_join_sale];
    $cash = round($row[mi_cash]*(100-$row[mi_cash_join_sale])/100);
}

$payment = mw_cash_to_payment($cash, $mw_cash[cf_tax])
?>
<script type="text/javascript" src="cybercash.js"></script>
<script type="text/javascript">
function account_move() {
    var msg = "";
    msg += "무통장입금은 관리자 확인이 필요하기 때문에 바로결제가 지원되지 않습니다.\n\n";
    msg += "<?=$mw_cash[cf_cash_name]?> 충전후 다시 가입신청 해주시기 바랍니다.\n\n";
    msg += "<?=$mw_cash[cf_cash_name]?> 충전 페이지로 이동하시겠습니까?";
    if (!confirm(msg)) return;

    window.open("<?=$mw_cash[path]?>");
    self.close();
}
</script>

<style type="text/css">
.membership-charge { padding:10px; }
.block { margin-bottom:20px; padding:10px; border:5px solid #efefef; }
.btn1 { background-color:#efefef; cursor:pointer; }
.block-title { font-weight:bold; font-size:13px; margin:0 0 10px 0; }
.block-image { float:left; margin:0 10px 10px 0; }
.block-content { line-height:20px; margin:0 0 10px 0; padding:0 0 0 10px; }
.block-content .stress { font-weight:bold; font-size:14px; margin:10px 0 10px 0; text-align:center; }
.block-content .blue { color:#0D6AB2; }
.block-hints { line-height:20px; color:#888; text-align:center; }
.block-hints a { color:#888; }
.block-button { text-align:center; margin:50px 0 20px 0;  }
.btn-image { cursor:pointer; }
</style>

<form name="fwrite" method="post" action="cash-charge-update.php">
<input type="hidden" name="ac_cash" value="<?=$cash?>">
<input type="hidden" name="paytype" value="">
<input type="hidden" name="ms_id" value="<?=$ms_id?>">
<input type="hidden" name="mi_day" value="<?=$mi_day?>">
<input type="hidden" name="membership" value="1">
<input type="hidden" name="bo_table" value="<?=$bo_table?>">
</form>

<div class="membership-charge">

<div class="block-title"><img src="img/icon_cash.gif" align="absmiddle"> <?=$g4[title]?></div>
<div class="block">
    <div class="block-content">

	<!--
        <div class="stress"> <?=$mw_cash[cf_cash_name]?> 결제 : <span class="blue"><?=number_format($cash)?> <?=$mw_cash[cf_cash_unit]?></span></div>
        -->
	<div class="stress"> 결제금액 : <span class="blue"><?=number_format($payment)?> 원</span></div>

	<div class="block-button">
	<? if ($mw_cash[cf_card]) { ?> <img src="img/bt_card.gif" class="btn-image" onclick="cash_membership_charge('card')"> <? } ?>
	<? if ($mw_cash[cf_bank]) { ?> <img src="img/bt_bank.gif" class="btn-image" onclick="cash_membership_charge('bank')"> <? } ?>
	<? if ($mw_cash[cf_hp]) { ?> <img src="img/bt_hp.gif" class="btn-image" onclick="cash_membership_charge('hp')"> <? } ?>
	<? if ($mw_cash[cf_account]) { ?> <img src="img/bt_account.gif" class="btn-image" onclick="account_move()"> <? } ?>
	</div>

    </div>
    <!--
    <div class="block-hints"> ※ <?="$mw_cash[cf_cash_name] 1$mw_cash[cf_cash_unit]당 ".number_format(mw_get_payment($mw_cash[cf_cash_price], $mw_cash[cf_tax]))." 원"?>
    <? if ($mw_cash[cf_tax]) echo "(부가세 포함)"; ?>  입니다.</div>
    -->
    <!--
    <? if ($mw_cash[cf_charge_min]) { ?>
    <div class="block-hints"> ※ 일반 최소충전금액은 <?=number_format(mw_get_payment($mw_cash[cf_charge_min], $mw_cash[cf_tax]))?> 원 <? if ($mw_cash[cf_tax]) echo "(부가세 포함)"; ?> 입니다.</div>
    <? } ?>

    <? if ($mw_cash[cf_card_min]) { ?>
    <div class="block-hints"> ※ 신용카드 최소충전금액은 <?=number_format(mw_get_payment($mw_cash[cf_card_min], $mw_cash[cf_tax]))?> 원 <? if ($mw_cash[cf_tax]) echo "(부가세 포함)"; ?> 입니다.</div>
    <? } ?>
    -->
</div>

</div>

<?
include_once("$g4[path]/tail.sub.php");
?>
